/*
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#ifndef _GOBJECTS_H
#define _GOBJECTS_H

#include <iostream>
#include <string>
#include <QModelIndex>
#include <QAbstractItemModel>
#include <QVariant>
#include <QList>
#include <QColor>
#include <QFont>
#include <QWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QTreeView>
#include <QAction>
#include <QToolButton>
#include <QItemSelectionModel>
#include <QModelIndex>
#include <QMessageBox>
#include <QHideEvent>
#include <QStringList>
#include <QItemDelegate>
#include <QDoubleSpinBox>
#include <QSpinBox>
#include <QSize>

#include "../gType.h"
#include "drawArea.h"
#include "bakalarka.h"
#include "assistant.h"

#define TABLE_VALUE tr("Stůl")
#define OBJECT_VALUE tr("Objekt")
#define FEED_VALUE tr("Feed (F)")
#define SPINDLE_VALUE tr("Spindle (S)")
#define NAME_VALUE tr("Jméno")
#define FORCE_RELATIVE tr("Relativně")

class treeItem{

	private:
		QList<treeItem *> childItems;
		QList<QVariant> itemData;
		treeItem *parentItem;
		
	public:
		treeItem(const QList<QVariant> &, treeItem * = 0);
		~treeItem();
		
		void appendChild(treeItem *);
		
		treeItem *child(int);
		int childCount() const;
		int columnCount() const;
		QVariant data(int) const;
		int row() const;
		treeItem *parent();
		void setData(const QVariant &, int);

};

class containerModel: public QAbstractItemModel{

	Q_OBJECT
	
	private:
		treeItem *rootItem, *tableItem;
		gObjectContainer *container;
		treeItem *activeItem;
		
		void setupModelData(gObjectContainer *, treeItem *);
		void replace(treeItem *, QVariant &);
		bool uniqueName(std::string &) const;
	
	signals:
		void changed(bool);
		
	public slots:
		void setActiveGObject(gObject *);
		void setActiveItem(treeItem *);
		
	public: 
		containerModel(gObjectContainer *, QObject * = 0);
		~containerModel();
		
		QVariant data(const QModelIndex &, int) const;
		Qt::ItemFlags flags(const QModelIndex &) const;
		QVariant headerData(int, Qt::Orientation, int = Qt::DisplayRole) const;
		QModelIndex index(int, int, const QModelIndex & = QModelIndex()) const;
		QModelIndex parent(const QModelIndex &) const;
		int rowCount(const QModelIndex & = QModelIndex()) const;
		int columnCount(const QModelIndex & = QModelIndex()) const;
		bool setData(const QModelIndex &, const QVariant &, int = Qt::EditRole);
		gObjectContainer *getContainer();
		QModelIndex searchObjectName(QModelIndex, QString &) const;

};

class doubleSpinBoxDelegate: public QItemDelegate{

	Q_OBJECT

	private:
		static const float MINIMUM = 0.0;
		static const float MAXIMUM = 100000.0;

	public:
		doubleSpinBoxDelegate(QObject * = 0);
		
		QWidget *createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const;
		
		virtual void setEditorData(QWidget *, const QModelIndex &) const;
		virtual void setModelData(QWidget *, QAbstractItemModel *, const QModelIndex &) const;
		virtual void updateEditorGeometry(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const;
		virtual QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const;

};

class gObjects: public QWidget{

	Q_OBJECT

	private:
		QAction *focus;
		QAction *mark;
		QAction *help;
		
		
		QVBoxLayout *vbox;
		QHBoxLayout *hbox;
		QTreeView *view;
		assistant *helpAssistant;
		doubleSpinBoxDelegate *delegate;
		QLineEdit *searchLine;
		
		const QWidget *getTopWidget() const;
		gObject *getObject(QModelIndex &);
		
		void createHandlers();
		void createActions();
		void createSearch();
	
	protected:
		void mouseDoubleClickEvent(QMouseEvent *);
	
	public slots:	
		void drawAreaMark();
		void drawAreaFocus();
		void showHelp();
		void searchObject();
		
	public:
		gObjects(QWidget * = 0);
		
		void setModel(containerModel *);
        containerModel *getModel();
		
};

#endif
